//user_server的测试客户端实现
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "etcd.hpp"
#include "channel.hpp" 
#include "utils.hpp"
#include "base.pb.h"
#include "user.pb.h"
//日志类参数
DEFINE_bool(log_mod,false,"true为发布模式,false为调试模式");
DEFINE_string(log_file,"","发布模式下日志存储的文件");
DEFINE_int32(log_level,0,"要输出的日志等级");
//服务注册参数
DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(basedir,"/service","服务查询的根目录");
DEFINE_string(user_service,"/service/user_service","当前实例名称");

zzy_mims::ServiceChannels::ChannelPtr channel;


// TEST(用户子服务测试,用户注册测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::UserRegisterReq request;
//     zzy_mims::UserRegisterRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_nickname("测试人员1号");
//     request.set_password("test1111");

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.UserRegister(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }
// TEST(用户子服务测试,用户登录测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::UserLoginReq request;
//     zzy_mims::UserLoginRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_nickname("新的测试人员1号");
//     request.set_password("test1111");

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.UserLogin(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     login_sid = response.login_session_id();
// }
// TEST(用户子服务测试,用户头像设置测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::SetUserAvatarReq request;
//     zzy_mims::SetUserAvatarRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_avatar("测试头像设置2");
//     request.set_user_id("8000346943a6-0000");
//     request.set_session_id(login_sid);

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.SetUserAvatar(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }
// TEST(用户子服务测试,用户昵称设置测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::SetUserNicknameReq request;
//     zzy_mims::SetUserNicknameRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_nickname("新的测试人员1号2");
//     request.set_user_id("8000346943a6-0000");
//     request.set_session_id(login_sid);

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.SetUserNickname(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }
// TEST(用户子服务测试,用户签名设置测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::SetUserDescriptionReq request;
//     zzy_mims::SetUserDescriptionRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_description("签名头像设置2");
//     request.set_user_id("8000346943a6-0000");
//     request.set_session_id(login_sid);

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.SetUserDescription(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }


// TEST(用户子服务测试,用户信息获取测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::GetUserInfoReq request;
//     zzy_mims::GetUserInfoRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_user_id("uid1");

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.GetUserInfo(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     ASSERT_EQ("uid1", response.user_info().user_id());
//     ASSERT_EQ("zzy", response.user_info().nickname());
//     ASSERT_EQ("O(∩_∩)O v3", response.user_info().description());
//     ASSERT_EQ("", response.user_info().phone());
//     ASSERT_EQ("", response.user_info().avatar());
// }
// TEST(用户子服务测试,用户信息批量获取测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::GetMultiUserInfoReq request;
//     zzy_mims::GetMultiUserInfoRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.add_users_id("uid1");
//     request.add_users_id("uid2");


//     //3.调用服务
//     brpc::Controller cntl;

//     stub.GetMultiUserInfo(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期

//     zzy_mims::UserInfo user_info1 = (*response.mutable_users_info())["uid1"];
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     ASSERT_EQ("uid1", user_info1.user_id());
//     ASSERT_EQ("zzy", user_info1.nickname());
//     ASSERT_EQ("O(∩_∩)O v3", user_info1.description());
//     ASSERT_EQ("", user_info1.phone());
//     ASSERT_EQ("", user_info1.avatar());

//     zzy_mims::UserInfo user_info2 = (*response.mutable_users_info())["uid2"];
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     ASSERT_EQ("uid2", user_info2.user_id());
//     ASSERT_EQ("uid2", user_info2.nickname());
//     ASSERT_EQ("O(∩_∩)O v2", user_info2.description());
//     ASSERT_EQ("17373xxxxxx", user_info2.phone());
//     ASSERT_EQ("", user_info2.avatar());
// }
// std::string sid;
// void send_verify_code()
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::PhoneVerifyCodeReq request;
//     zzy_mims::PhoneVerifyCodeRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_phone_number("17373124944");

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.GetPhoneVerifyCode(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     sid =  response.verify_code_id();

// }
// TEST(用户子服务测试,使用手机号注册用户测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::PhoneRegisterReq request;
//     zzy_mims::PhoneRegisterRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_phone_number("17373124944");
//     send_verify_code();
//     request.set_verify_code_id(sid);
//     std::cout<<"手机号注册,请输入验证码:";
//     std::string code;
//     std::cin>>code;
//     request.set_verify_code(code);
    
//     //3.调用服务
//     brpc::Controller cntl;

//     stub.PhoneRegister(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }
// TEST(用户子服务测试,使用手机号登录用户测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::PhoneLoginReq request;
//     zzy_mims::PhoneLoginRsp response; 
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_phone_number("17373124944");
//     send_verify_code();
//     request.set_verify_code_id(sid);
//     std::cout<<"手机号登录,请输入验证码:";
//     std::string code;
//     std::cin>>code;
//     request.set_verify_code(code);

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.PhoneLogin(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
//     login_sid = response.login_session_id();
// }
// TEST(用户子服务测试,手机号设置测试)
// {
//     //1.获取rpc服务调用句柄
//     ::zzy_mims::UserService_Stub stub(channel.get());
//     //2.填写请求
//     zzy_mims::SetUserPhoneNumberReq request;
//     zzy_mims::SetUserPhoneNumberRsp response;
    
//     request.set_request_id(zzy_mims::uuid());
//     request.set_phone_number("17373124944");
//     request.set_user_id("eb26de6bdba5-0002");
//     request.set_session_id(login_sid);
//     send_verify_code();
//     request.set_phone_verify_code_id(sid);
//     std::cout<<"手机号登录,请输入验证码:";
//     std::string code;
//     std::cin>>code;
//     request.set_phone_verify_code(code);

//     //3.调用服务
//     brpc::Controller cntl;

//     stub.SetUserPhoneNumber(&cntl,&request,&response,nullptr);//同步调用
//     //4.验证应答是否符合预期
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(response.success());
// }


std::string login_sid;
void user_rig(const std::string& name,const std::string& pswd)
{
    //1.获取rpc服务调用句柄
    ::zzy_mims::UserService_Stub stub(channel.get());
    //2.填写请求
    zzy_mims::UserRegisterReq request;
    zzy_mims::UserRegisterRsp response;
    
    request.set_request_id(zzy_mims::uuid());
    request.set_nickname(name);
    request.set_password(pswd);

    //3.调用服务
    brpc::Controller cntl;

    stub.UserRegister(&cntl,&request,&response,nullptr);//同步调用
    //4.验证应答是否符合预期
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(response.success());
}
void user_login(const std::string& name,const std::string& pswd)
{
    //1.获取rpc服务调用句柄
    ::zzy_mims::UserService_Stub stub(channel.get());
    //2.填写请求
    zzy_mims::UserLoginReq request;
    zzy_mims::UserLoginRsp response;
    
    request.set_request_id(zzy_mims::uuid());
    request.set_nickname(name);
    request.set_password(pswd);

    //3.调用服务
    brpc::Controller cntl;

    stub.UserLogin(&cntl,&request,&response,nullptr);//同步调用
    //4.验证应答是否符合预期
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(response.success());
    login_sid =response.login_session_id();
}
void user_set_avatar(const std::string& uid,const std::string& content,const std::string& login_sid)
{
    //1.获取rpc服务调用句柄
    ::zzy_mims::UserService_Stub stub(channel.get());
    //2.填写请求
    zzy_mims::SetUserAvatarReq request;
    zzy_mims::SetUserAvatarRsp response;
    
    request.set_request_id(zzy_mims::uuid());
    request.set_avatar(content);
    request.set_user_id(uid);
    request.set_session_id(login_sid);

    //3.调用服务
    brpc::Controller cntl;

    stub.SetUserAvatar(&cntl,&request,&response,nullptr);//同步调用
    //4.验证应答是否符合预期
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(response.success());
}
int main(int argc,char* argv[])
{ 
    testing::InitGoogleTest(&argc, argv);

    google::ParseCommandLineFlags(&argc,&argv,true);
    zzy_mims::init_logger(FLAGS_log_mod,FLAGS_log_file,(spdlog::level::level_enum)FLAGS_log_level);
    //关闭brpc的默认日志输出
    logging::LoggingSettings logset;
    logset.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(logset);

    //1.创建RPC信道管理对象
    auto scm = std::make_shared<zzy_mims::ServChanManager>();
    scm->Declared(FLAGS_user_service);
    //2.构建服务发现对象
    auto online = std::bind(&zzy_mims::ServChanManager::ServiceOnline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    auto offline = std::bind(&zzy_mims::ServChanManager::ServiceOffline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    std::shared_ptr<zzy_mims::Discovery> dclient = std::make_shared<zzy_mims::Discovery>(FLAGS_etcd_host,FLAGS_basedir,online,offline); 
    //获取信道
    channel = scm->choose(FLAGS_user_service);
    if(!channel)
    {
        LOG_ERROR("获取用户rpc服务调用信道失败\n");
        return -1;
    }
    // user_rig("测试人员1","123456");
    // user_rig("测试人员2","123456");
    // user_rig("测试人员3","123456");
    // user_rig("测试人员4","123456");
    // user_rig("测试人员5","123456");


    user_login("测试人员1","123456");
    user_set_avatar("6559d28e9440-0000","测试人员1的头像",login_sid);
    user_login("测试人员2","123456");
    user_set_avatar("158998af4180-0001","测试人员2的头像",login_sid);
    user_login("测试人员3","123456");
    user_set_avatar("5966d78949e3-0002","测试人员3的头像",login_sid);
    user_login("测试人员4","123456");
    user_set_avatar("3892b567a3bb-0003","测试人员4的头像",login_sid);
    user_login("测试人员5","123456");
    user_set_avatar("22cb9cc825db-0004","测试人员5的头像",login_sid);
    return 0;
}